//
//  NewsNet.m
//  SportSocial
//
//  Created by yull on 14/12/13.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "NewsNet.h"

@implementation NewsNet

+ (AFHTTPRequestOperation *)getTopNewsWithSuccessBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/topNews",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)paginateNewsWithPageNumber:(NSInteger)pageNumber
                                              pageSize:(NSInteger)pageSize
                                          successBlock:(httpSuccessBlock)successBlock
                                             failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/paginateNews",[self baseUrl]];
    
    NSDictionary *param = @{@"page_number":[NSNumber numberWithInt:pageNumber],
                            @"page_size":[NSNumber numberWithInt:pageSize]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)getTopKnowLedgeWithSuccessBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/topKnowLedge",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)paginateKnowLedgeWithPageNumber:(NSInteger)pageNumber
                                                   pageSize:(NSInteger)pageSize
                                               successBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/paginateKnowLedge",[self baseUrl]];
    
    NSDictionary *param = @{@"page_number":[NSNumber numberWithInt:pageNumber],
                            @"page_size":[NSNumber numberWithInt:pageSize]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;

}

//分页去评论
+ (AFHTTPRequestOperation *)getNewsCommentWithNewsId:(UInt64)newsId
                                             pageNum:(NSInteger)pageNum
                                            pageSize:(NSInteger)pageSize
                                        successBlock:(httpSuccessBlock)successBlock
                                           failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/paginateComment",[self baseUrl]];
    NSDictionary *param = @{@"news_id":[NSNumber numberWithLongLong:newsId],
                            @"page_number":[NSNumber numberWithInteger:pageNum],
                            @"page_size":[NSNumber numberWithInteger:pageSize]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

//发表评论
+ (AFHTTPRequestOperation *)sendNewsCommentWithNewsId:(UInt64)newsId
                                              comment:(NSString *)comment
                                         successBlock:(httpSuccessBlock)successBlock
                                            failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/postComment",[self baseUrl]];
    
    NSDictionary *param = @{@"token":[self token],
                            @"news_id":[NSNumber numberWithLongLong:newsId],
                            @"comment":comment};
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)isNewsFavoriteWithNewsId:(UInt64)newsId
                                        successBlock:(httpSuccessBlock)successBlock
                                           failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/isFavorite", [self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                             @"news_id":[NSNumber numberWithLongLong:newsId]};
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation, error);
    }];
    return operation;
}

//加入收藏
+ (AFHTTPRequestOperation *)addNewsToFavoriteWithNewsId:(UInt64)newsId
                                           successBlock:(httpSuccessBlock)successBlock
                                              failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/addToFavorite",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"news_id":[NSNumber numberWithLongLong:newsId]};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation, error);
    }];
    return operation;
}

//移除收藏
+ (AFHTTPRequestOperation *)removeNewsFromFavoriteWithNewsId:(UInt64)newsId
                                                successBlock:(httpSuccessBlock)successBlock
                                                   failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/deleteFromFavorite",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"news_id":[NSNumber numberWithLongLong:newsId]};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation, error);
    }];
    return operation;
}

//收藏的新闻
+ (AFHTTPRequestOperation *)getNoticeNewsWithSuccessBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{    
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/listFavoriteNews",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

//收藏的小知识
+ (AFHTTPRequestOperation *)getNoticeKnowLedgeWithSuccessBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/news/listFavoriteKnowLedge",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}
@end
